import binascii
import os
from datetime import datetime


def now_datetime_str():
    """格式化的时间字符串"""
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def char2hex(input_str):
    s = input_str
    for i in s:
        if ("Z" >= i >= "A") or ("z" >= i >= "a") or ("9" >= i >= "0") or "%" == i or "(" == i or ")" == i or "-" == i:
            continue
        else:
            str_16 = str(binascii.b2a_hex(i.encode('utf-8'))).split('\'')[1].upper()  # 字符串转16进制
            hex_str = '%'
            for j in range(len(str_16)):
                if j > 0 and j % 2 == 0:
                    hex_str += '%'
                hex_str += str_16[j]
            s = s.replace(i, hex_str)
    return s


def message_head(message_type):
    if 0 == message_type:
        txt = now_datetime_str() + "\t\t进程：【" + str(os.getpid()) + "】\t\t"
    elif 1 == message_type:
        txt = now_datetime_str() + "\t\t进程错误：【" + str(os.getpid()) + "】\t\t"
    else:
        txt = ''
    return txt
